package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.INavService;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.PathEnum;
import com.cn.wanxi.util.Tool;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NavServiceImpl implements INavService {


    @Override
    public List<NavModel> getNavModels() {
//        1.创建文件,地址，文件夹，文件名
//        File file = IO.createNote(Path.PATH, Path.TEMP, Path.FILE_NAME_NAV);
//        2.封装dao层数据
        List list = getNavDaoList();
//        3.写入数据
        IO.writeNote(list, PathEnum.FILE_NAME_NAV.getValue());
//        4.读取数据
        String result = IO.readNote();
//        5.封装数据
        List<NavModel> navModelList = setModelList(result);
        return navModelList;
    }

    private List<NavModel> setModelList(String result) {
        String[] results = result.split(PathEnum.SPLIT.getValue());
//        for (String value : results
//        ) {
//            System.out.println(value);
//        }
        String title = results[0];
//        title = title.replace("[", "");
//        title = title.replace("]", "");
//        title = title.replace(" ", "");
//        title= Tool.replace(title);
        String[] titles = Tool.replace(title).split(",");
        String href = results[1];
//        href = href.replace("[", "");
//        href = href.replace("]", "");
//        href = href.replace(" ", "");
        String[] hrefs = Tool.replace(href).split(",");
        List<NavModel> list = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            NavModel model = new NavModel();
            model.setHref(hrefs[i]);
            model.setTitle(titles[i]);
            list.add(model);
        }
        return list;
    }


    private List getNavDaoList() {
        List<String> list = new ArrayList<>();
        NavDao navDao = new NavDao();
        list.add(Arrays.toString(navDao.getTitle()));
        list.add(Arrays.toString(navDao.getHref()));

        return list;
    }


//    private File createNote(String path, String temp, String fileName) {
//        File directory = new File(path, temp);
//        directory.mkdirs();
//        File file = new File(directory, fileName);
//        try {
//            file.createNewFile();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return file;
//    }
}
